//
//  STANetworkNetworkUtil.m
//  STANetworkHelper
//
//  Created by jerry on 2019/5/5.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STANetworkNetworkUtil.h"
#import "STANetworkKeyChainStore.h"
#define KEY_NETWORK_USERNAME_PASSWORD @"com.ogawa.STLNetworkUtil.password"

@implementation STANetworkNetworkUtil

/**
 获取当前时间戳
 @return 时间戳 （以毫秒为单位）
 */
+ (NSString *)getNowTimeTimestamp{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate dateWithTimeIntervalSinceNow:0];//现在时间,你可以输出来看下是什么格式
    NSTimeInterval timeInterval = [datenow timeIntervalSince1970]*1000;
    NSString *timeSp = [NSString stringWithFormat:@"%.f", timeInterval];
    
    return timeSp;
}

/**
 获取UUID
 @return UUID
 */
+(NSString *)getUUID{
    NSString * strUUID = (NSString *)[STANetworkKeyChainStore load:KEY_NETWORK_USERNAME_PASSWORD];
    
    //首次执行该方法时，uuid为空
    if ([strUUID isEqualToString:@""] || !strUUID)
    {
        //生成一个uuid的方法
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        
        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
        
        //将该uuid保存到keychain
        [STANetworkKeyChainStore save:KEY_NETWORK_USERNAME_PASSWORD data:strUUID];
    }
    
    return strUUID;
}

/**
 字典转json字符串
 @param dict 字典
 @return json串
 */
+ (NSString *)dictionaryToJson:(NSDictionary *)dict{
    
    if (dict == nil) {
        return nil;
    }
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:nil error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    //    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    //    NSRange range = {0,jsonString.length};
    //    //去掉字符串中的空格
    //    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    //    NSRange range2 = {0,mutStr.length};
    //    //去掉字符串中的换行符
    //    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return jsonString;
}

/**
 拼接字段参与签名的地址
 @param subUrl 地址
 @param parameter 入参字段
 @return 参与签名的地址
 */
+ (NSString *)matchSuburl:(NSString *)subUrl paramter:(NSDictionary *)parameter{
    
    for (NSInteger i = parameter.count-1; i >= 0; i--) {
        if (i == parameter.count - 1) {
            NSString *stringValue = [self stringValue:parameter.allValues[i]];
            subUrl = [subUrl stringByAppendingString:[NSString stringWithFormat:@"?%@=%@", parameter.allKeys[i], stringValue]];
        }else{
            NSString *stringValue = [self stringValue:parameter.allValues[i]];
            subUrl = [subUrl stringByAppendingString:[NSString stringWithFormat:@"&%@=%@", parameter.allKeys[i], stringValue]];
        }
    }
    return subUrl;
}
+ (NSString *)stringValue:(id)value
{
    NSString *valueStirng = @"";
    if (![value isKindOfClass:[NSString class]]) {
        valueStirng = [value stringValue];
    }else{
        valueStirng = value;
    }
    NSArray *stringArray = [valueStirng componentsSeparatedByString:@","];
    NSString *stringValue = @"";
    NSInteger index = 0;
    for (NSString *string in stringArray) {
        if (index == 0) {
            stringValue = string;
        }else{
            stringValue = [stringValue stringByAppendingString:[NSString stringWithFormat:@"%%2C%@", string]];
        }
        index ++;
    }
    return stringValue;
}

@end
